﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace IBOBO_Controls
{
    public class Function
    {
        #region 进度条
        /// <summary>
        /// 显示进度窗体
        /// </summary>
        /// <param name="frmThis">当前窗体</param>
        /// <param name="maxNum">最大进度数</param>
        /// <param name="msg">进度文本信息</param>
        /// <param name="td">当前线程</param>
        /// <param name="topMost">是否顶层显示</param>
        /// <param name="showCancelButton">是否显示取消按钮</param>
        public static void ShowProcessBar(Form frmThis, int maxNum, string msg, Thread td, bool topMost = true, bool showCancelButton = true)
        {
            frmThis.Invoke(new EventHandler(delegate
            {
                _frmProcessBar frm = new _frmProcessBar(td);

                frm.TopMost = topMost;
                frm.prb.Maximum = maxNum;
                frm.lblMsg.Text = msg;
                frm.Name = "ibobo_process_" + frmThis.Name;
                frm.Show();
            }));
        }

        /// <summary>
        /// 执行一次进度
        /// </summary>
        /// <param name="frmThis">当前窗体</param>
        public static void ProcessBarPerformStep(Form frmThis)
        {
            frmThis.Invoke(new EventHandler(delegate
            {
                if (Application.OpenForms["ibobo_process_" + frmThis.Name] != null)
                {
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).prb.PerformStep();
                }
            }));
        }

        /// <summary>
        /// 执行一次进度
        /// </summary>
        /// <param name="frmThis">当前窗体</param>
        /// <param name="maxNum">最大进度数</param>
        public static void ProcessBarPerformStep(Form frmThis, int maxNum)
        {
            frmThis.Invoke(new EventHandler(delegate
            {
                if (Application.OpenForms["ibobo_process_" + frmThis.Name] != null)
                {
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).prb.Maximum = maxNum;
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).prb.PerformStep();
                }
            }));
        }

        /// <summary>
        /// 执行一次进度
        /// </summary>
        /// <param name="frmThis">当前窗体</param>
        /// <param name="msg">进度文本信息</param>
        public static void ProcessBarPerformStep(Form frmThis, string msg)
        {
            frmThis.Invoke(new EventHandler(delegate
            {
                if (Application.OpenForms["ibobo_process_" + frmThis.Name] != null)
                {
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).lblMsg.Text = msg;
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).prb.PerformStep();
                }
            }));
        }

        /// <summary>
        /// 执行一次进度
        /// </summary>
        /// <param name="frmThis">当前窗体</param>
        /// <param name="msg">进度文本信息</param>
        /// <param name="maxNum">最大进度数</param>
        public static void ProcessBarPerformStep(Form frmThis, string msg, int maxNum)
        {
            frmThis.Invoke(new EventHandler(delegate
            {
                if (Application.OpenForms["ibobo_process_" + frmThis.Name] != null)
                {
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).prb.Maximum = maxNum;
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).lblMsg.Text = msg;
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).prb.PerformStep();
                }
            }));
        }

        /// <summary>
        /// 关闭进度窗体
        /// </summary>
        /// <param name="frmThis">当前窗体</param>
        public static void CloseProcessBar(Form frmThis)
        {
            frmThis.Invoke(new EventHandler(delegate
            {
                if (Application.OpenForms["ibobo_process_" + frmThis.Name] != null)
                {
                    ((_frmProcessBar)(Application.OpenForms["ibobo_process_" + frmThis.Name])).Dispose();
                }
            }));
        }
        #endregion
    }
}
